#pragma once

#include "Protocol.hpp"

enum
{
    Div_Zero = 1,
    Mod_zero,
    Other
};

class ServerCalculator
{
public:
    Response AuxiliaryCalculation(const Request &req) // 不会对其修改，可用const修饰和用引用
    {
        Response resp(0, 0);
        switch (req.oper_)
        {
        case '+':
            resp.result_ = req.data1_ + req.data2_;
            break;
        case '-':
            resp.result_ = req.data1_ - req.data2_;
            break;
        case '*':
            resp.result_ = req.data1_ * req.data2_;
            break;
        case '/':
        {
            if (req.data2_ == 0)
                resp.code_ = Div_Zero;
            else
                resp.result_ = req.data1_ / req.data2_;
        }
        break;
        case '%':
        {
            if (req.data2_ == 0)
                resp.code_ = Mod_zero;
            else
                resp.result_ = req.data1_ % req.data2_;
        }
        break;
        default:
            resp.code_ = Other;
            break;
        }
        return resp;
    }

    //"len"\n"content"\n
    std::string Calculate(std::string &package)
    {
        std::string content;
        bool b = Decapsulation(package, &content);
        if (!b)
            return "";
        Request req;
        b = req.Deserialize(content);
        if (!b)
            return "";
        Response resp = AuxiliaryCalculation(req);
        content = "";
        resp.Serialize(&content);
        content = Encapsulation(content);
        return content;
    }
};